package com.jsjosem.aftersales.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.data.response.WorkTicket;
import com.thinkcool.circletextimageview.CircleTextImageView;

import java.util.List;
import java.util.Objects;

public class WorkTicketItemAdapter extends RecyclerView.Adapter<WorkTicketItemAdapter.TicketViewHolder> {

    private final Context mContext;

    private final List<WorkTicket> mWorkTickets;

    private OnItemClickListener mOnItemClickListener;


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public WorkTicketItemAdapter(Context mContext, List<WorkTicket> mWorkTickets) {
        this.mContext = mContext;
        this.mWorkTickets = mWorkTickets;
    }

    @NonNull
    @Override
    public TicketViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.work_tickets_item_list, parent, false);
        TicketViewHolder ticketViewHolder = new TicketViewHolder(view);
        view.setOnClickListener(v -> {
            if (Objects.nonNull(mOnItemClickListener)) {
                mOnItemClickListener.onItemClick(v, ticketViewHolder.getAdapterPosition());
            }
        });
        return ticketViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull TicketViewHolder holder, int position) {
        holder.ticketTypeTv.setText(mWorkTickets.get(position).getTicketType());
        holder.customerNameTv.setText(mWorkTickets.get(position).getCustomerName());
        holder.ticketCodeTv.setText(mWorkTickets.get(position).getTicketCode());
        holder.ticketStateTv.setText(mWorkTickets.get(position).getTicketState());
        holder.ticketAddressTv.setText(mWorkTickets.get(position).getAddress());
        holder.scheduleTimeTv.setText(mWorkTickets.get(position).getScheduleTime());
        if (mWorkTickets.get(position).getHandlers()!=null){
            holder.handlersHeadImg.setVisibility(View.VISIBLE);
            holder.handlersHeadImg.setText(mWorkTickets.get(position).getHandlers());
        }
    }

    @Override
    public int getItemCount() {
        return mWorkTickets.size();
    }

    static class TicketViewHolder extends RecyclerView.ViewHolder {

        private final TextView ticketTypeTv;

        private final TextView customerNameTv;

        private final TextView ticketCodeTv;

        private final TextView ticketStateTv;

        private final TextView ticketAddressTv;

        private final TextView scheduleTimeTv;

        private final TextView handlersHeadImg;

        public TicketViewHolder(@NonNull View itemView) {
            super(itemView);
            ticketTypeTv = itemView.findViewById(R.id.ticket_type_tv);
            customerNameTv = itemView.findViewById(R.id.customer_name_value_tv);
            ticketCodeTv = itemView.findViewById(R.id.ticket_code_value_tv);
            ticketStateTv = itemView.findViewById(R.id.ticket_state);
            ticketAddressTv = itemView.findViewById(R.id.ticket_address_value_tv);
            scheduleTimeTv = itemView.findViewById(R.id.schedule_time_value_tv);
            handlersHeadImg = itemView.findViewById(R.id.handlers_head_img);
        }
    }
}
